# coding: utf8

import os
import logging
import re
import json
import traceback
import requests_html
from multiprocessing.pool import ThreadPool


"""中国科学技术大学
国家示范性微电子学院
"""

logging.basicConfig(level=logging.INFO)

session = requests_html.HTMLSession()
pool = ThreadPool(os.cpu_count() * 2)


pos_ptn = re.compile(r"(教授|副教授|助理教授|研究员|副研究员|博士导师|硕士导师|博士生研究生|硕士生导师|IEEE Fellow|IEEE\s*会员|博士|讲师|教师|高级工程师|工程师)")
ri_ptn = re.compile(r'研究(?:方向|领域|重点)(?:为|包括)*[:：\n]*([ \n\S]+?(。|\n{2,}))')


def teacher_list():
    try:
        # 微电子工程系
        urls = [
            "https://sme.ustc.edu.cn/ys/list.htm",
            "https://sme.ustc.edu.cn/jswwwyjy/list.htm",
            "https://sme.ustc.edu.cn/fjswwwfyjy/list.htm",
            "https://sme.ustc.edu.cn/js/list.htm",
            "https://sme.ustc.edu.cn/trfyjy/list.htm",
            "https://sme.ustc.edu.cn/bsh/list.htm"
        ]

        teachers = []
        for _url in urls:
            r = session.get(_url)
            if r.status_code == 200:
                lis = r.html.xpath("//div[@id='wp_news_w29'][1]//a")

                for a in lis:
                    name = a.full_text.split()[0]
                    url = list(a.absolute_links)[0]

                    teachers.append((name, url))

                    logging.info("name: %s, url: %s", name, url)
                pass
            else:
                logging.warning("status code error: %s", r.status_code)
                r.raise_for_status()
            pass  # end for
        pool.imap_unordered(teacher_detail, teachers)
    except Exception as e:
        traceback.print_exc()
        raise e


def teacher_detail(payload):
    name, url = payload
    logging.info("loading %s ...", name)

    try:
        r = session.get(url)
        if r.status_code == 200:
            avatar = r.html.xpath("//div[@class='photo'][1]//img[1]/@src")
            avatar = r.html.base_url + avatar[0] if avatar else ''

            email = r.html.xpath("//div[@class='team_right'][1]//h2[1]/a[1]/text()")
            email = "".join(email) if email else ''

            edu = r.html.xpath("//div[@class='team_right_jl_m'][1]//text()")
            edu = " ".join(edu) if edu else ''

            intro = r.html.xpath("//div[@class='team_txt'][1]//p")
            intro = "\n".join([p.full_text.strip() for p in intro])

            pos = set(pos_ptn.findall(edu + "\n" + intro))
            if '教授' in pos:
                pos.discard('助理教授')
                pos.discard('副教授')
                pos.discard('研究员')
                pos.discard('副研究员')
                pos.discard('讲师')
                pos.discard('教师')
            if '副教授' in pos:
                pos.discard('研究员')
                pos.discard('副研究员')
                pos.discard('讲师')
                pos.discard('教师')
            if '研究员' in pos:
                pos.discard('副研究员')

            pos = ','.join(list(pos))

            ri = ri_ptn.search(intro)
            ri = ri.group(1).strip() if ri else ''

            logging.info("name: %s, pos: %s, email: %s, edu: %s, ri: %s, avatar: %s", name, pos, email, edu, ri, avatar)
            pass
        else:
            logging.warning("status code error: %s", r.status_code)
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


if __name__ == '__main__':
    teacher_list()

    pool.close()
    pool.join()

    session.close()

    pass

